package com.itaem.community.modules.infrastructure.authority.mapper.role;

import com.itaem.community.modules.infrastructure.authority.dto.permission.PermissionBriefDTO;
import com.itaem.community.modules.infrastructure.authority.dto.role.RoleBriefDTO;
import com.itaem.community.modules.infrastructure.authority.dto.role.RoleCreateDTO;
import com.itaem.community.modules.infrastructure.authority.dto.role.RoleDTO;
import com.itaem.community.modules.infrastructure.authority.dto.role.RoleUpdateDTO;
import com.itaem.community.modules.infrastructure.authority.entity.permission.PermissionEntity;
import com.itaem.community.modules.infrastructure.authority.entity.role.RoleEntity;
import com.itaem.community.modules.infrastructure.authority.entity.role.RolePermissionEntity;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;

import java.lang.annotation.Target;
import java.util.List;

/**
 * <p>项目名称: community </p>
 * <p>文件名称: RoleTransformer </p>
 * <p>描述: [类型描述] </p>
 * <p>创建时间: 2020/11/1 </p>
 * <p>作者信息: tomato</p>
 *
 * @author tomato
 * @version v1.0
 */
@Mapper(componentModel = "spring")
public interface RoleTransformer {

    /**
     * 提取数据库信息到数据传输实体上
     *
     * @param entity 数据库信息
     * @return 简要角色信息数据传输实体
     */
    RoleBriefDTO entityToBriefDto(RoleEntity entity);

    /**
     * 提取数据库信息到数据传输实体上
     *
     * @param entity      数据库信息
     * @param permissions 权限信息
     * @return 角色信息数据传输实体
     */
    RoleDTO entityToDto(RoleEntity entity, List<PermissionBriefDTO> permissions);

    /**
     * 提取更新信息到数据库实体中
     *
     * @param updateDto 更新信息
     * @return 数据库实体
     */
    @Mapping(target = "deleted", expression = "java(Boolean.FALSE)")
    RoleEntity updateDtoToEntity(RoleUpdateDTO updateDto);

    /**
     * 从前端提交的数据中创建数据库实体
     *
     * @param createDto 前端提交的数据
     * @return 数据库实体
     */
    @Mapping(target = "id", ignore = true)
    @Mapping(target = "deleted", expression = "java(Boolean.FALSE)")
    RoleEntity createDtoToEntity(RoleCreateDTO createDto);

    /**
     * 组装链接实体
     *
     * @param roleEntity       角色实体信息
     * @param permissionEntity 权限实体信息
     * @return 中间表实体
     */
    @Mapping(target = "id", ignore = true)
    @Mapping(target = "roleId", source = "roleEntity.id")
    @Mapping(target = "permissionId", source = "permissionEntity.id")
    RolePermissionEntity toLinkEntity(RoleEntity roleEntity, PermissionEntity permissionEntity);
}
